import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import { login, getInfo, logout } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

storage.addPlugin(expirePlugin)
const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo)
          .then((response) => {
            // const result = response.result
            console.log('result', response)
            const csrfToken = response.data.csrfToken
            sessionStorage.setItem(ACCESS_TOKEN, csrfToken)
            localStorage.setItem('_sync_qjt_csrf_', csrfToken) // 新的csrf同步到其他页面
            commit('SET_TOKEN', csrfToken)
            resolve(response)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        // 请求后端获取用户信息 /api/user/info
        getInfo()
          .then((response) => {
            const data = response.data
            console.log('GetInfo', data)
            if (data) {
              //   const role = { ...data.role }
              //   role.permissions = data.role.permissions.map((permission) => {
              //     const per = {
              //       ...permission,
              //       actionList: (permission.actionEntitySet || {}).map((item) => item.action),
              //     }
              //     return per
              //   })
              //   role.permissionList = role.permissions.map((permission) => {
              //     return permission.permissionId
              //   })
              //   // 覆盖响应体的 role, 供下游使用
              //   data.role = role
              commit('SET_ROLES', [])
              commit('SET_INFO', data)
              commit('SET_NAME', { name: data.account, welcome: welcome() })
              commit('SET_AVATAR', data.avatar)
              // 下游
              resolve(data)
            } else {
              reject(new Error('getInfo: roles must be a non-null array !'))
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token)
          .then(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            sessionStorage.setItem(ACCESS_TOKEN, '')
            sessionStorage.clear()
            localStorage.clear()
            resolve()
          })
          .catch((err) => {
            console.log('logout fail:', err)
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            sessionStorage.setItem(ACCESS_TOKEN, '')
            sessionStorage.clear()
            localStorage.clear()
            resolve()
          })
          .finally(() => {})
      })
    },
  },
}

export default user
